package com.lyh.hkcloud.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import com.lyh.hkcloud.MainActivity;
import com.lyh.hkcloud.constants.*;
import com.lyh.hkcloud.util.base.SaveData;

public class NetworkDialog extends AlertDialog {
    Context context;

    public NetworkDialog(Context context) {
        super(context);
        this.context = context;
    }

    public void open() {
        final UrlConstants.UrlItem[] items = UrlConstants.urlItems();
        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
        alertBuilder.setTitle("请选择网络环境！");
        alertBuilder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                UrlConstants.UrlItem item = items[i];

                UrlConstants.BASE_URL = item.baseUrl;
                UrlConstants.BUS_ROOT = item.busUrl;
                UrlConstants.PRINT_ROOT = item.printUrl;
                UrlConstants.WMS_ROOT = item.wmsUrl;
                UserInfo.networkName = item.name;
                SaveData.putValue(context, "BASE_URL", UrlConstants.BASE_URL);
                SaveData.putValue(context, "NETWORK_NAME", UserInfo.networkName);
                SaveData.putValue(context, "BUS_ROOT", UrlConstants.BUS_ROOT);
                SaveData.putValue(context, "PRINT_ROOT", UrlConstants.PRINT_ROOT);
                SaveData.putValue(context, "WMS_ROOT", UrlConstants.WMS_ROOT);
                NetworkDialog.this.dismiss();
                context.startActivity(new Intent(context, MainActivity.class));
                ((Activity) context).finish();
            }
        });
        alertBuilder.create().show();
    }


}
